package com.lihua.project.provider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wuyuj
 * 三方免费Api提供者。
 */
@Component
public class ShowApiProvider {
    @Value("${otherApi.appId}")
    private String appId;
    @Value("${otherApi.sign}")
    private String sign;

    /**
     * 获取励志名言
     * https://www.showapi.com/
     * @param count 每次获取几条
     * @return 返回名言，json字符串
     */
    public JSONArray getSaying(int count){
        String requestUrl = "https://route.showapi.com/1211-1?count="+count+"&showapi_appid="+appId+"&showapi_sign="+sign ;
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(requestUrl, String.class);
        try {
            return (JSONArray)JSON.parseObject(forObject).getJSONObject("showapi_res_body").get("data");
        }catch (ClassCastException e){
            return null;
        }
    }

    /**
     * 随机获取一张图片的范围地址
     * 接口地址：https://qqlykm.cn/api/bing/get?type=rand
     * type	string	否	为空则跳转到当天的壁纸
     * type=rand,info	string	否	随机选择7天内的壁纸并显示或版权显示
     * @return 返回一个图片的url
     * */
    public String getPicture(){
        String requestUrl = "https://qqlykm.cn/api/bing/get?type=rand";
        RestTemplate restTemplate = new RestTemplate();
        try {
            restTemplate.getForObject(requestUrl, String.class);
            return "";
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 获取网易新闻
     * T1348647853363 头条 http://c.m.163.com/nc/article/headline/T1348647853363/0-40.html
     * T1467284926140 精选 http://c.3g.163.com/nc/article/list/T1467284926140/0-20.html
     * */
    public String getNetEaseNews(String key){
        //头条
        String requestUrl = "http://c.m.163.com/nc/article/headline/T1348647853363/0-40.html";
        if ("T1467284926140".equals(key)){
            //精选
            requestUrl = "http://c.3g.163.com/nc/article/list/T1467284926140/0-20.html";
        }
        
        RestTemplate restTemplate = new RestTemplate();
        try {
            String forObject = restTemplate.getForObject(requestUrl, String.class);
            return forObject;
        }catch (Exception e){
            return null;
        }
    }

    public static void main(String[] args) {
        String forObject = "artiList({\"BBM54PGAwangning\":[{\"stitle\":\"\",\"docid\":\"H4THKAD80515CCSC\",\"digest\":\"4月13日，俄罗斯国防部称，乌克兰海军陆战队第36旅的102\",\"source\":\"国防时报排头兵\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/32e6223dp00rab7ft003vc0009c0070c.png\",\"title\":\"俄公布乌军\\\"投降\\\"画面 加入乌军的英国男子跟母亲诀别\",\"priority\":100,\"ptime\":\"2022-04-14 11:01:27\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4THKAD80515CCSC.html\",\"commentCount\":3555},{\"stitle\":\"\",\"docid\":\"H4TIN7NF00018AP1\",\"digest\":\"（观察者网讯）塞尔维亚从中国购买的FK-3防空导弹系统交付之\",\"source\":\"观察者网\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/6d73f363p00rab6w0009ac0009c0070c.png\",\"title\":\"塞尔维亚购买中国武器 德国借\\\"加入欧盟\\\"一事发出威胁\",\"priority\":100,\"ptime\":\"2022-04-14 10:27:14\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4TIN7NF00018AP1.html\",\"commentCount\":478},{\"stitle\":\"\",\"docid\":\"H4S716E200019SNS\",\"digest\":\"新冠疫情严峻，东方网记者刘晓晶近期核酸检测呈阳性，目前已被转\",\"source\":\"东方网\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/c0c02a68p00rab5a7003zc0009c0070c.png\",\"title\":\"上海记者感染转运至方舱:病友凌晨争吵 护士不敢动弹\",\"priority\":100,\"ptime\":\"2022-04-14 10:21:42\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4S716E200019SNS.html\",\"commentCount\":17757},{\"stitle\":\"\",\"docid\":\"H4TEQPFK0552G199\",\"digest\":\"马里乌波尔的战事打得怎么样？俄乌13日对此有不同的说法，到底\",\"source\":\"枢密院十号\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/e08b9557p00rab4k5002kc0009c0070c.png\",\"title\":\"上千乌军投降了还是突围了?西方分析人士更信俄方说法\",\"priority\":100,\"ptime\":\"2022-04-14 10:08:02\",\"hasImg\":1,\"url\":\"\",\"commentCount\":12025},{\"stitle\":\"\",\"docid\":\"H4T6C00F0514R9OJ\",\"digest\":\"【环球时报特约记者张一帆柳玉鹏环球时报记者刘扬】编者的话：随\",\"source\":\"环球网资讯\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/ff76cc16p00rab13b0014c0009c0070c.png\",\"title\":\"胜利日来临 俄军加强对亚速营大本营攻势:\\\"困死\\\"乌军\",\"priority\":100,\"ptime\":\"2022-04-14 08:43:02\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4T6C00F0514R9OJ.html\",\"commentCount\":18104},{\"stitle\":\"\",\"docid\":\"H4T6C0I40514R9OJ\",\"digest\":\"台湾《中国时报》4月13日文章，原题：运20远程运送大型武器\",\"source\":\"环球网资讯\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/cfb0027cp00raayhy001ic0009c0070c.png\",\"title\":\"运-20飞7500公里赴欧运送大型武器 台媒：震撼\",\"priority\":100,\"ptime\":\"2022-04-14 08:00:02\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4T6C0I40514R9OJ.html\",\"commentCount\":4749},{\"stitle\":\"\",\"docid\":\"H4T9BG330001899O\",\"digest\":\"当地时间14日，俄罗斯国防部发布消息称，俄黑海舰队旗舰“莫斯\",\"source\":\"央视新闻客户端\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/fc14183ep00rab47x0032c000s600e3c.png\",\"title\":\"重磅：俄黑海舰队旗舰发生爆炸 疑被乌军导弹重创\",\"priority\":100,\"ptime\":\"2022-04-14 07:43:32\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4T9BG330001899O.html\",\"commentCount\":194136},{\"stitle\":\"\",\"docid\":\"H4T6C0D90514R9OJ\",\"digest\":\"【环球时报记者隋鑫萧达柳直任重】“拜登为俄罗斯在乌克兰的行动\",\"source\":\"环球网资讯\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/6bc559abp00raay0l00apc000hk00aoc.png\",\"title\":\"美准备与俄斗到只剩最后一个乌克兰人?普京:确实如此\",\"priority\":100,\"ptime\":\"2022-04-14 07:39:28\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4T6C0D90514R9OJ.html\",\"commentCount\":10813},{\"stitle\":\"\",\"docid\":\"H4T8SGMS052182V3\",\"digest\":\"资料图究竟什么样的魔力，让拜登把乌克兰说成了中国？要知道，中\",\"source\":\"牛弹琴\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/845c2d71p00raaydg002tc0009c0070c.png\",\"title\":\"牛弹琴:究竟什么样的魔力 让拜登把乌克兰说成中国?\",\"priority\":100,\"ptime\":\"2022-04-14 07:35:22\",\"hasImg\":1,\"url\":\"\",\"commentCount\":4601},{\"stitle\":\"\",\"docid\":\"H4SB9JUR00018AP1\",\"digest\":\"【文/观察者网齐倩】当地时间4月13日，塞尔维亚总统武契奇发\",\"source\":\"观察者网\",\"imgsrc\":\"http://cms-bucket.ws.126.net/2022/0414/7e8e1d6dp00raaxlf00czc0009c0070c.png\",\"title\":\"武契奇回应购买中国FK-3防空导弹:透明合规 不像北约\",\"priority\":100,\"ptime\":\"2022-04-14 07:18:16\",\"hasImg\":1,\"url\":\"https://3g.163.com/news/article/H4SB9JUR00018AP1.html\",\"commentCount\":21024}]})4635\n" +
                "\n";
        String pattern = "(artiList\\()(.*?)(\\)\\d+)";
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(forObject);
        if (matcher.find()){
            //获取指定组中的字符串
            System.out.println(matcher.group(2));
        }
    }
}
